const md5 = require("@/libs/md5/index.js");

// 请求加密
function reqMd5(request){
    let timestamp = Math.round(new Date().getTime()/1000);
    let sign = md5(`Timestamp${timestamp}${JSON.stringify(request.data)}`);
    // 设置时间戳
    request.header['Timestamp'] = "1656577384";
    request.header['Sign'] = "8EF3259DDF9C9B7653027B26D313C648";
}

// 请求日志打印
function reqlog(request){
	if (process.env.NODE_ENV === 'development') {
		// #ifndef APP-PLUS
		console.log(
			`%c 请求地址 %c ${request.method} %c ${request.url}`,
			'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
			'background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff',
			'background:transparent'
		)
		Object.keys(request.data).forEach(key => {
			console.log(
				`%c 请求参数 %c ${key}:${request.data[key]}`,
				'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
				'background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff',
			)
		})
		// #endif
		// #ifdef APP-PLUS
		console.log(`请求地址 ${request.url}`);
		// #endif
	}
}

// 响应日志打印
function reslog(){

}

// 判断arr是否为一个数组，返回一个bool值
function isArray (arr) {
    return Object.prototype.toString.call(arr) === '[object Array]';
}

function encodeParams(params, isPrefix = true) {
    let prefix = isPrefix ? '?' : ''
    let _result = []
    for (let key in params) {
        let value = params[key]
        // 去掉为空的参数
        if (['', undefined, null].includes(value)) {
            continue
        }
        if (value.constructor === Array) {
            value.forEach(_value => {
                _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
            })
        } else {
            _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
        }
    }
    return _result.length ? prefix + _result.join('&') : ''
}

// 深度克隆
function deepClone (obj) {
	// 对常见的“非”值，直接返回原来值
	if([null, undefined, NaN, false].includes(obj)) return obj;
    if(typeof obj !== "object" && typeof obj !== 'function') {
		//原始类型直接返回
        return obj;
    }
    var o = isArray(obj) ? [] : {};
    for(let i in obj) {
        if(obj.hasOwnProperty(i)){
            o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
        }
    }
    return o;
}

// JS对象深度合并
function deepMerge(target = {}, source = {}) {
	target = deepClone(target);
	if (typeof target !== 'object' || typeof source !== 'object') return false;
	for (var prop in source) {
		if (!source.hasOwnProperty(prop)) continue;
		if (prop in target) {
			if (typeof target[prop] !== 'object') {
				target[prop] = source[prop];
			} else {
				if (typeof source[prop] !== 'object') {
					target[prop] = source[prop];
				} else {
					if (target[prop].concat && source[prop].concat) {
						target[prop] = target[prop].concat(source[prop]);
					} else {
						target[prop] = deepMerge(target[prop], source[prop]);
					}
				}
			}
		} else {
			target[prop] = source[prop];
		}
	}
	return target;
}

export default {
	reqlog,
    reqMd5,
    deepClone,
    deepMerge,
    encodeParams
}
